package com.cloud.picture.controller;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.picture.annotation.AuthCheck;
import com.cloud.picture.common.BaseResponse;
import com.cloud.picture.constant.UserConstants;
import com.cloud.picture.exception.BusinessException;
import com.cloud.picture.model.dto.DeleteRequest;
import com.cloud.picture.model.dto.user.*;
import com.cloud.picture.model.dto.user.message.MessageAddRequest;
import com.cloud.picture.model.dto.user.message.MessageListRequest;
import com.cloud.picture.model.entity.User;
import com.cloud.picture.model.enums.ErrorCodeEnum;
import com.cloud.picture.model.vo.FollowerVO;
import com.cloud.picture.model.vo.LoginUserVO;
import com.cloud.picture.model.vo.MessageVO;
import com.cloud.picture.model.vo.UserVO;
import com.cloud.picture.service.UserService;
import com.cloud.picture.utils.ResultUtils;
import com.cloud.picture.utils.ThrowUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author syf
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 发送验证码
     */
    @PostMapping("/sendEmail")
    @ApiOperation(value = "发送验证码")
    @ApiOperationSupport(order = 1)
    public BaseResponse<Boolean> sendEmail(@RequestBody UserEmailRequest userEmailRequest) {
        if (userEmailRequest == null || CharSequenceUtil.hasBlank(userEmailRequest.getEmail())) {
            throw new BusinessException(ErrorCodeEnum.PARAMS_ERROR, "参数为空");
        }
        return userService.sendVerificationCode(userEmailRequest.getEmail());
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    @ApiOperationSupport(order = 2)
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        // 1. 校验
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCodeEnum.PARAMS_ERROR, "参数为空");
        // 2. 注册
        String userEmail = userRegisterRequest.getUserEmail();
        String code = userRegisterRequest.getCode();
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        long result = userService.userRegister(userAccount,userEmail, userPassword, checkPassword, code);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    @ApiOperationSupport(order = 3)
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCodeEnum.PARAMS_ERROR, "参数为空");
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/get/login")
    @ApiOperation(value = "获取当前登录用户信息")
    @ApiOperationSupport(order = 4)
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(loginUser));
    }

    /**
     * 用户注销
     */
    @PostMapping("/logout")
    @ApiOperation(value = "用户注销")
    @ApiOperationSupport(order = 5)
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCodeEnum.PARAMS_ERROR);
        boolean res = userService.userLogout(request);
        return ResultUtils.success(res);
    }

    /**
     * 修改个人信息
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改个人信息")
    @ApiOperationSupport(order = 6)
    public BaseResponse<Boolean> editUser(@RequestBody UserInfoUpdateRequest userInfoUpdateRequest) {
        ThrowUtils.throwIf(userInfoUpdateRequest == null, ErrorCodeEnum.PARAMS_ERROR);
        Boolean res = userService.userInfoUpdate(userInfoUpdateRequest);
        ThrowUtils.throwIf(ObjectUtil.isEmpty(res), ErrorCodeEnum.OPERATION_ERROR);
        return ResultUtils.success(res);
    }

    /**
     * 创建用户
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "创建用户")
    @ApiOperationSupport(order = 7)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest == null, ErrorCodeEnum.PARAMS_ERROR);
        Long res = userService.addUser(userAddRequest);
        ThrowUtils.throwIf(ObjUtil.isEmpty(res), ErrorCodeEnum.OPERATION_ERROR);
        return ResultUtils.success(res);
    }

    /**
     * 根据 id 获取用户
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "根据 id 获取用户")
    @ApiOperationSupport(order = 8)
    public BaseResponse<User> getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCodeEnum.PARAMS_ERROR);
        User user = userService.getUserById(id);
        ThrowUtils.throwIf(user == null, ErrorCodeEnum.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * 根据 id 获取包装类
     */
    @GetMapping("/get/vo")
    @ApiOperation(value = "根据 id 获取包装类")
    @ApiOperationSupport(order = 9)
    public BaseResponse<UserVO> getUserVoById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCodeEnum.PARAMS_ERROR);
        UserVO userVO = userService.getUserVoById(id);
        ThrowUtils.throwIf(userVO == null, ErrorCodeEnum.NOT_FOUND_ERROR);
        return ResultUtils.success(userVO);
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "删除用户")
    @ApiOperationSupport(order = 10)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCodeEnum.PARAMS_ERROR);
        boolean res = userService.deleteUser(deleteRequest);
        return ResultUtils.success(res);
    }

    /**
     * 更新用户
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "更新用户")
    @ApiOperationSupport(order = 11)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        ThrowUtils.throwIf(userUpdateRequest == null || userUpdateRequest.getId() == null, ErrorCodeEnum.PARAMS_ERROR);
        User user = User.builder().build();
        BeanUtils.copyProperties(userUpdateRequest, user);
        boolean res = userService.updateById(user);
        ThrowUtils.throwIf(!res, ErrorCodeEnum.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 分页获取用户列表
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "分页获取用户列表")
    @ApiOperationSupport(order = 12)
    public BaseResponse<Page<UserVO>> listUserVoByPage(@RequestBody UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest ==  null, ErrorCodeEnum.PARAMS_ERROR);
        long current = userQueryRequest.getCurrent();
        long size = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<>(current, size), userService.getQueryWrapper(userQueryRequest));
        Page<UserVO> userVOPage = new Page<>(current, size, userPage.getTotal());
        List<UserVO> userVOList = userService.getUserVOList(userPage.getRecords());
        userVOPage.setRecords(userVOList);
        return ResultUtils.success(userVOPage);
    }

    /**
     * 关注/取消关注
     */
    @PostMapping("/follow")
    @ApiOperation(value = "关注/取消关注")
    @ApiOperationSupport(order = 13)
    public BaseResponse<Boolean> followUser(@RequestBody UserFollowRequest userFollowRequest) {
        ThrowUtils.throwIf(userFollowRequest == null, ErrorCodeEnum.PARAMS_ERROR);
        Boolean res = userService.userFollow(userFollowRequest);
        return ResultUtils.success(res);
    }

    /**
     * 发私信
     */
    @PostMapping("/message/send")
    @ApiOperation(value = "发私信")
    @ApiOperationSupport(order = 16)
    public BaseResponse<Boolean> sendMessage(@RequestBody MessageAddRequest messageAddRequest) {
        return null;
    }

    /**
     * 获取私信列表
     */
    @PostMapping("/message/list")
    @ApiOperation(value = "获取私信列表")
    @ApiOperationSupport(order = 17)
    public BaseResponse<Page<MessageVO>> getMessageByPage(@RequestBody MessageListRequest messageListRequest) {
        return null;
    }

    /**
     * 获取关注自己的列表
     */
    @PostMapping("/follow/follow")
    @ApiOperation(value = "获取关注自己的列表")
    @ApiOperationSupport(order = 14)
    public BaseResponse<Page<FollowerVO>> getFollowerByPage(@RequestBody UserFollowRequest userFollowRequest) {
        return null;
    }

    /**
     * 获取自己关注的列表
     */
    @PostMapping("/follow/following")
    @ApiOperation(value = "获取自己关注的列表")
    @ApiOperationSupport(order = 15)
    public BaseResponse<Page<FollowerVO>> getFollowingByPage(@RequestBody UserFollowRequest userFollowRequest) {
        return null;
    }
}
